package com.example.shipload_e.update_tool;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class UpdateParser {

	public static VersionEntity parser(String jsonString) {
		VersionEntity versionEntity = new VersionEntity();

		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(jsonString);

			String changeLog = jsonObject.getString("changeLog");
			JSONArray apkUrl = jsonObject.getJSONArray("apkUrl");
			int number = apkUrl.length();
			String[] url = new String[number];
			long[] length = new long[number];
			double[] version = new double[number];
			JSONArray apkNumber = jsonObject.getJSONArray("fileLength");
			JSONArray versions = jsonObject.getJSONArray("version");

			for(int i = 0;i < number;i++) {
				length[i] = Long.parseLong(apkNumber.getString(i));

			}

			for(int i = 0;i < number;i++) {
				url[i] = apkUrl.getString(i);

			}
			for(int i = 0;i < number;i++) {
				version[i] = Double.parseDouble(versions.getString(i));

			}
			versionEntity.setApkUrl(url);
			versionEntity.setFileLength(length);
			versionEntity.setNewVersion(version);
			versionEntity.setChangeLog(changeLog);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return versionEntity;
	}

}
